package com.web.server.common.response;

import com.web.server.common.exception.StatusCode;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 响应，约定格式
 * <p/>
 * //    data:[
 * //
 * //            ],
 * //    page:{
 * //        pageNo:1,
 * //                pageSize:10,
 * //                totalCount:34007,
 * //                pageCount:3401
 * //    },
 * //    header:{},
 * //    code:1,
 * //    message:"成功！"
 */
public class Response implements Serializable {

    // 响应状态码：1  （缺省值）
    private int code = StatusCode.STATUS_SUCCESS;
    // 响应状态： "成功" （缺省值）
    private String message = StatusCode.message(StatusCode.STATUS_SUCCESS);

    private PageNavigation page = null;
    private Object data = null;

    public Response() {

    }
    public Response(Object data) {
        this.data = data;
    }

    public Response(Map<?, ?> data) {
        this.data = data;
    }

    public Response(List<?> data) {
        this.data = data;
    }

    public Response(Page<?> page) {
        this.data = page.getData();
        this.page = new PageNavigation(page.getPageSize(), page.getPageNo(), page.getTotalCount(), page.getPageCount());
    }

    public Response(int code) {
        this.code = code;
        this.message = StatusCode.message(code);
    }

    public Response(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public long getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
        this.message = StatusCode.message(code);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setCodeAndMessage(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public PageNavigation getPage() {
        return page;
    }

    public void setPage(PageNavigation page) {
        this.page = page;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        if (data instanceof Page) {
            setPageDate((Page) data);
        } else {
            this.data = data;
        }
    }

    private void setPageDate(Page<?> page) {
        this.data = page.getData();
        this.page = new PageNavigation(page.getPageSize(), page.getPageNo(), page.getTotalCount(), page.getPageCount());
    }

    private static class PageNavigation implements Serializable {
        private int pageNo = 1;
        private int pageSize = 10;
        private int totalCount = 0;
        private int pageCount = 0;

        private PageNavigation(int pageSize, int pageNo, int totalCount, int pageCount) {
            this.pageNo = pageNo;
            this.pageSize = pageSize;
            this.pageCount = pageCount;
            this.totalCount = totalCount;
        }

        public int getPageNo() {
            return pageNo;
        }

        public void setPageNo(int pageNo) {
            this.pageNo = pageNo;
        }

        public int getPageSize() {
            return pageSize;
        }

        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }

        public int getTotalCount() {
            return totalCount;
        }

        public void setTotalCount(int totalCount) {
            this.totalCount = totalCount;
        }

        public int getPageCount() {
            return pageCount;
        }

        public void setPageCount(int pageCount) {
            this.pageCount = pageCount;
        }
    }
}
